﻿#region FileHeader
//////////////////////////////////////////////////////////////////////////////////
// Author:                          Hector Llanos
// Date:                            08.27.2010
// FileName:                        CPlayer2.cs
// Reason:                          second player
//////////////////////////////////////////////////////////////////////////////////
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;


using Run_Game;
using MessagingSystem;
using Event_System;
using Base;
using AnimatedSprite;
using Globals;

namespace Objects
{
    public class CPlayer2 : CBase, IListener
    {
        private float m_fBulletDelay;
        private Vector2 m_vnewVel;
        private AnimatedTexture m_pHurtAnimation;
        private bool m_bHit;

        private CCharacterBase m_pcharacter;

        public CCharacterBase Character
        {
            get { return m_pcharacter; }
            set
            {
                m_pcharacter = value;
                Animtion.Load(Game1.Instance.Content, m_pcharacter.Filename, m_pcharacter.MaxFramesX, m_pcharacter.MaxFramesY, m_pcharacter.FPS);
            }
        }

        public CPlayer2() : base(Game1.Instance.Content, "SpriteSheets/player", 8, 1, 10)
        {
            Type = type.Player2;

            // the animations should be states, at this point
            // we will just copy the same animation and add a
            // red flash :/
            m_pHurtAnimation = new AnimatedTexture(Animtion);
            m_bHit = false;
            m_pHurtAnimation.Color = Color.Red;

            //register for being hurt
            CEventSystem.Instance.RegisterClient("Player2Hurt", this);


            //add Player2's special item
            AddFlags(Items.PEWPEW | Items.DUCK);
        }
        ~CPlayer2()
        {
            CEventSystem.Instance.UnregisterClient("Player2Hurt", this);
        }
        public override void Input()
        {
            //movement
            if (Keyboard.GetState().IsKeyDown(Keys.NumPad2))
            {
                m_vnewVel += new Vector2(0, 1f * Speed);
            }
            if (Keyboard.GetState().IsKeyDown(Keys.NumPad4))
            {
                m_vnewVel += new Vector2(-1f * Speed, 0);
            }
            if (Keyboard.GetState().IsKeyDown(Keys.NumPad6))
            {
                m_vnewVel += new Vector2(1f * Speed, 0);
            }
            if (Keyboard.GetState().IsKeyDown(Keys.NumPad8))
            {
                m_vnewVel += new Vector2(0, -1f * Speed);
            }

            //update the new vel
            Velocity = m_vnewVel;

            //shoot shit!!
            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                if (m_fBulletDelay >= 0.5f)
                {
                    m_fBulletDelay = 0.0f;
                    CMessageSystem.Instance.SendMessage(new CCreateBulletMessage(this, type.BASEBULLET));
                }
            }
            //base.Input();
        }
        public override void Update(float fElapsedTime)
        {
            //if the player isn't moving
            // then stop animations
            if (Velocity == new Vector2(0, 0))
            {
                Animtion.Pause();
                m_pHurtAnimation.Pause();
            }
            else
            {
                Animtion.Play();
                m_pHurtAnimation.Play();
            }
            m_fBulletDelay += fElapsedTime;
            m_vnewVel = new Vector2(0, 0);

            if (Position.X + Width / 2 > CGlobal.Screenwidth)
                PositionX = 0;

            if (Position.X < 0)
                PositionX = CGlobal.Screenwidth - Width / 2;

            if (Position.Y < 0)
                PositionY = CGlobal.Screenheight - Height / 2;

            if (Position.Y + Height / 2 > CGlobal.Screenheight)
                PositionY = 0;

            //update the animation for the player
            Animtion.UpdateFrame(fElapsedTime);

            //update the hurt animation
            m_pHurtAnimation.UpdateFrame(fElapsedTime);
            m_pHurtAnimation.Rotation = Heading;

            base.Update(fElapsedTime);
        }
        public override void Render()
        {
            base.Render();

            //render the player's animation
            Animtion.DrawFrame(Game1.Instance.SpriteBatch, Position);

            //render the hurt animation if we are hurt
            if (m_bHit)
            {
                m_pHurtAnimation.DrawFrame(Game1.Instance.SpriteBatch, Position);
                m_bHit = false;
            }
        }
        public override bool CheckCollision(CBase pBase)
        {
            //if the player shoots me
            if (base.CheckCollision(pBase) && pBase.Type == type.ENEMYBULLET)
            {
                //get hurt
                CBulletBase tempBullet = (CBulletBase)pBase;
                Character.Health -= tempBullet.Damage;

                //let the hud know our health changed
                CMessageSystem.Instance.SendMessage(new CPlayer2HealthChange(Character.Health, Character.MaxHealth));

                //broadcast that we were hurt :(
                CEventSystem.Instance.SendEvent("Player2Hurt", this);

                //delete the bullet that hit us
                CMessageSystem.Instance.SendMessage(new CDestroyBulletMessage(pBase));

                return true;
            }

            return false;
        }
        public void HandleEvent(CEvent pEvent)
        {

            if (pEvent.EventID == "Player2Hurt" && pEvent.Param == this)
            {
                //just set the hurt bool to true
                m_bHit = true;
            }
        }
    }
}
